/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import dao.TourCateDAO;
import dao.TourDAO;
import dao.UserDAO;
import dto.Tour;
import dto.TourCategory;
import dto.User;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import util.Function;

/**
 *
 * @author lab
 */
public class AdminAction extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String action = request.getParameter("action");
            //String url = "";
            String page = request.getParameter("page");
            if (page == null && action == null) {
                response.sendRedirect("administrator.jsp");
            }
            // <editor-fold defaultstate="collapsed" desc="page = tourCate. Click on the + sign on the left to edit the code.">
            if (page.equals("tourCate")) {
                TourCateDAO tourCateDAO = new TourCateDAO();
                if (action.equals("manage")) {
                    ArrayList<TourCategory> tourCates = new ArrayList<TourCategory>();
                    tourCates = tourCateDAO.getAllTourCate();
                    request.setAttribute("tourCates", tourCates);
                    request.setAttribute("action", null);
                    response.sendRedirect("administrator/administrator.jsp?page=tourCate&action=add");
                }
                if (action.equals("add")) {
                    TourCategory tourCate = new TourCategory();
                    String tourCateName = request.getParameter("tourCateName").toString();
                    String description = request.getParameter("description").toString();
                    tourCate.setCateName(tourCateName);
                    tourCate.setDescription(description);
                    if (tourCate.validate()) {
                        if (tourCateDAO.createTourCategory(tourCate)) {
                            out.println(Function.thongBao(tourCateDAO.mesage, "../AdminAction?page=tourCate&action=manage"));
                        } else {
                            out.println(Function.thongBaoAndGoBack(tourCateDAO.mesage));
                        }
                    } else {
                        out.println(Function.thongBaoAndGoBack("Hay dien day du thong tin"));
                    }
                }
                if (action.equals("edit")) {
                    TourCategory tourCate = new TourCategory();
                    tourCate.setTourCateID(Integer.parseInt(request.getParameter("tourCateID")));
                    tourCate.setCateName(request.getParameter("tourCateName").toString());
                    tourCate.setDescription(request.getParameter("description").toString());
                    if (tourCate.validate()) {
                        if (tourCateDAO.updateTourCate(tourCate)) {
                            out.println(Function.thongBao(tourCateDAO.mesage, "../AdminAction?page=tourCate&action=manage"));
                        } else {
                            out.println(Function.thongBaoAndGoBack(tourCateDAO.mesage));
                        }
                    } else {
                        out.println(Function.thongBaoAndGoBack("Hay dien day du thong tin"));
                    }
                }
                if (action.equals("delete")) {

                    if (request.getParameter("tourCateID") != null) {
                        int tourCateID = Integer.parseInt(request.getParameter("tourCateID"));
                        if (tourCateDAO.deleteTourCate(tourCateID)) {
                            out.println(Function.thongBao(tourCateDAO.mesage, "../AdminAction?page=tourCate&action=manage"));
                        } else {
                            out.println(Function.thongBaoAndGoBack(tourCateDAO.mesage));
                        }
                    } else {
                        out.println(Function.thongBaoAndGoBack("Co loi xay ra"));
                    }
                }

            }
            // </editor-fold>



            // <editor-fold defaultstate="collapsed" desc="page = tour. Click on the + sign on the left to edit the code.">
            if (page.equals("tour")) {
                TourDAO tourDAO = new TourDAO();
                if (action.equals("manage")) {
                    ArrayList<Tour> tours = new ArrayList<Tour>();
                    tours = tourDAO.getAllTour();
                    request.setAttribute("tours", tours);
                    response.sendRedirect("administrator/administrator.jsp?page=tour&action=add");
                }
                if (action.equals("add")) {
                    Tour tour = new Tour();
                    TourCategory tourCate = null;
                    if (!request.getParameter("category").toString().equals("")) {
                        int tourCateID = Integer.parseInt(request.getParameter("category").toString());
                        TourCateDAO tourCateDAO = new TourCateDAO();
                        tourCate = tourCateDAO.getTourCateByID(tourCateID);

                    }
                    tour.setTourCate(tourCate);
                    tour.setName(request.getParameter("tourName"));
                    tour.setImageUrl(null);
                    tour.setPrice(Double.parseDouble(request.getParameter("price")));
                    tour.setDescription(request.getParameter("description"));
                    if (tour.validate()) {
                        if (tourDAO.createTour(tour)) {
                            out.println(Function.thongBao(tourDAO.mesage, "../AdminAction?page=tour&action=manage"));

                        } else {
                            out.println(Function.thongBaoAndGoBack(tourDAO.mesage));
                        }
                    } else {
                        out.println(Function.thongBaoAndGoBack("Hay dien day du thong tin"));

                    }
                }
                if (action.equals("edit")) {
                    Tour tour = new Tour();
                    TourCategory tourCate = null;
                    tour.setTourID(Integer.parseInt(request.getParameter("tourID")));
                    if (!request.getParameter("category").toString().equals("")) {
                        int tourCateID = Integer.parseInt(request.getParameter("category").toString());
                        TourCateDAO tourCateDAO = new TourCateDAO();
                        tourCate = tourCateDAO.getTourCateByID(tourCateID);

                    }
                    tour.setTourCate(tourCate);
                    tour.setName(request.getParameter("tourName").toString());
                    tour.setImageUrl(null);
                    tour.setPrice(Double.parseDouble(request.getParameter("price")));
                    tour.setDescription(request.getParameter("description").toString());
                    if (tour.validate()) {
                        if (tourDAO.updateTour(tour)) {
                            out.println(Function.thongBao(tourDAO.mesage, "../AdminAction?page=tour&action=manage"));
                        } else {
                            out.println(Function.thongBaoAndGoBack(tourDAO.mesage));
                        }
                    } else {
                        out.println(Function.thongBaoAndGoBack("Hay dien day du thong tin"));
                    }
                }

                if (action.equals("delete")) {
                    if (request.getParameter("tourID") != null) {
                        int tourID = Integer.parseInt(request.getParameter("tourID"));
                        if (tourDAO.deleteTour(tourID)) {
                            out.println(Function.thongBao(tourDAO.mesage, "../AdminAction?page=tour&action=manage"));
                        } else {
                            out.println(Function.thongBaoAndGoBack(tourDAO.mesage));
                        }
                    } else {
                        out.println(Function.thongBaoAndGoBack("Co loi xay ra"));
                    }
                }

            }
            // </editor-fold>
            // <editor-fold defaultstate="collapsed" desc="page = user. Click on the + sign on the left to edit the code.">
            if (page.equals("user")) {
                UserDAO userDAO = new UserDAO();
                if (action.equals("manage")) {
                    ArrayList<User> users = new ArrayList<User>();
                    users = userDAO.getAllUser();
                    request.setAttribute("users", users);
                    response.sendRedirect("administrator/administrator.jsp?page=user&action=edit");
                }
                if (action.equals("edit")) {
                    User user = null;
                    if (request.getParameter("username") != null) {
                        String username = request.getParameter("username").toString();
                        user = userDAO.getUserByUserName(username);

                    }
                    user.setRoleNo(Integer.parseInt(request.getParameter("roleNo").toString()));
                    user.setStatus(Boolean.valueOf(request.getParameter("status").toString()));
                    if (user.validate()) {
                        if (userDAO.updateUser(user)) {
                            out.println(Function.thongBao(userDAO.mesage, "AdminAction?page=user&action=manage"));
                        } else {
                            out.println(Function.thongBaoAndGoBack(userDAO.mesage));
                        }
                    } else {
                        out.println(Function.thongBaoAndGoBack("Hay dien day du thong tin"));
                    }

                }
                if (action.equals("delete")) {
                    if (request.getParameter("username") != null) {
                        String username = request.getParameter("username").toString();
                        if (userDAO.deleteUser(username)) {
                            out.println(Function.thongBao(userDAO.mesage, "AdminAction?page=user&action=manage"));
                        } else {
                            out.println(Function.thongBaoAndGoBack(userDAO.mesage));
                        }
                    } else {
                        out.println(Function.thongBaoAndGoBack("Co loi xay ra"));
                    }
                }
            }
            // </editor-fold>
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet AdminAction</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet AdminAction at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
             */
        } finally {
            out.close();
        }
    }

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);


    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);


    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";

    }// </editor-fold>
}
